<?php

namespace App\Http\Controllers;

use App\Machine;
use Illuminate\Http\Request;


class MachineController extends Controller
{
    public function getAll() {
       $machine=Machine::all();
        return response()->json($machine);
    }
    public function getOneWorkshop($id) {
       $machine=Machine::where('workshopId',$id)->get();
        return response()->json($machine);
    }
    public function getCurrent($page) {
        $pages=$page*15-15;
        $machine=Machine::skip($pages)->take(15)->get();
        return response()->json($machine);
    }
    public function one($id) {
        $machine=Machine::find($id);
        return response()->json($machine);
    }

    public function getCount() {
        $machine=Machine::all()->count();
        return response()->json($machine);
    }
    public function destroy($id)
    {
        Machine::destroy($id);
        return response()->json('删除成功');
    }



    public function search($word,$type)
    {
        $machine=Machine::where($type,$word)->get();
        return response()->json($machine);
    }

    public function add(Request $request)
    {
        $addMachine=$request->except('_token');
//        $addMachine['machineId']=$request->input('machineId');
//        $machine=Machine::find($addMachine['machineId']);
//        $addMachine['machineName']=$machine->machineName;
        Machine::create($addMachine);
        return response()->json('添加成功');
    }



    public function update(Request $request, $id)
    {
        $updateMachine=$request->except('_token');
//        $updateMachine['machineId']=$request->input('machineId');
//        $machine=Machine::find($updateMachine['machineId']);
//        $updateMachine['machineName']=$machine->machineName;

        Machine::where('id',$id)->update($updateMachine);
        return response()->json('修改成功');
    }


}
